package com.iot.ht.job;

import com.iot.core.manager.job.IMyJob;
import com.iot.core.springboot.ApplicationUtils;
import com.iot.core.util.MyDateTime;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ObjectUtils;

@Slf4j
@DisallowConcurrentExecution
public class PrintJob implements IMyJob {
    @Override
    public String getJobName() {
        return "PrintJob";
    }

    @Override
    public String getJobGroup() {
        return "Group1";
    }

    @Override
    public String getJobDesc() {
        return "测试";
    }

    @Override
    public String getCron() {
        return "*/30 * * * * ?";//5秒
    }

    StringRedisTemplate redisTemplate;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        if(ObjectUtils.isEmpty(redisTemplate)){
            redisTemplate= ApplicationUtils.getBean(StringRedisTemplate.class);
        }
        var b=redisTemplate.opsForValue().setIfAbsent(this.getJobGroup(),"1" );
        if(b){
            try{

                log.info( this.getJobName()+":"+ MyDateTime.now() );
            }
            catch (Exception ex){

            }
            finally {
                redisTemplate.delete( this.getJobGroup() );
            }
        }

    }
}
